/**
 * 2016-5-10 弹窗消息修改版
 * v1.1 author Smile 1085332235@qq.com
 * 
 */
(function($){
	$.suMessager = function(options,callback){
		$.suMessager.defaults = {
			version:'2.0'
			,layer:{//窗口大小
				width:300,
				height:200,
				zindex:100//上下层级
			}
			,title:'推荐信息'//标题
			,content:''//文本内容
			,time:3000//自动隐藏延迟时间
			,anims:{
				type:'slide',//动画效果类型
				speed:600//速度
			}
			,domid:'suMessageId'//窗口ID，必填，必须唯一
			,bindwin:'body'//绑定父级窗口
		}
		var opts = $.extend({},$.suMessager.defaults,options);
		
		//初始化函数
		this.inits = function(opts){
			var layer = opts.layer
				,title = opts.title
				,content = opts.content
				,domid = opts.domid
				,type = opts.anims.type
				,speed = opts.anims.speed
				,bindwin = opts.bindwin;
			
			if ($("#"+domid).is("div")) {//防止重复ID
				return;
			}
			
			$(bindwin).prepend(
				'<div id="'+domid+'" class="suWin_bm" style="border:#b9c9ef 1px solid;z-index:'+layer.zindex+';width:'
						+ layer.width
						+ 'px;height:'
						+ layer.height
						+ 'px;position:absolute; display:none;background:#cfdef4; bottom:0; right:0; overflow:hidden;"><div style="border:1px solid #fff;border-bottom:none;width:100%;height:25px;font-size:12px;overflow:hidden;color:#1f336b;"><span style="float:right;padding:5px 5px 5px 0;width:16px;line-height:20px;color:#1f336b;font-size:20px;text-align:center;cursor:pointer;overflow:hidden;"onclick="$.suMessager.closefunc(this,\''+ type + '\',' + speed + ')">☒</span><div style="padding:5px 0 5px 5px;width:100px;line-height:18px;text-align:left;overflow:hidden;">'
						+ title
						+ '</div><div style="clear:both;"></div></div> <div style="padding-bottom:5px;border:1px solid #fff;border-top:none;width:100%;height:auto;font-size:12px;"><div id="message_content" style="margin:0 5px 0 5px;border:#b9c9ef 1px solid;padding:10px 0 10px 5px;font-size:12px;width:'
						+ (layer.width - 17)
						+ 'px;height:'
						+ (layer.height - 54)
						+ 'px;color:#1f336b;text-align:left;overflow-y:auto;overflow-x:hidden;">'
						+ content + '</div><!--<div style="width:100%;height:20px;font-size:12px;text-align:center"><a style="display:inline-block;text-decoration:none;line-height:18px;color:#000;padding:0 10px;border:1px solid #8DA8E8;margin-top:2px;" href="javascript:;" onclick="$.suMessager.closefunc(this,\''
						+ type + '\',' + speed + ')">关闭</a></div>--></div></div>');
			$(bindwin=='body'?window:bindwin).scroll(function(event) {
				var bottomHeight =  "-"+(document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop);
				
				if($(bindwin).is('div')){
					bottomHeight =  "-"+$("#"+domid).offsetParent().scrollTop();
				}
				bottomHeight =  "-"+$("#"+domid).offsetParent().scrollTop();
				$("#"+domid).css("bottom", bottomHeight + "px");
			});
		}
		
		//隐藏函数
		this.closefun = function(opts,stime) {
			stime = stime ? stime : 0;
			switch (opts.anims.type) {
				case 'slide':
					setTimeout(function(){
						$("#"+opts.domid).slideUp(opts.anims.speed);
					},stime);
					break;
				case 'fade':
					setTimeout(function(){
						$("#"+opts.domid).fadeOut(opts.anims.speed);
					},stime);
					break;
				case 'show':
					setTimeout(function(){
						$("#"+opts.domid).hide(opts.anims.speed);
					},stime);
					break;
				default:
					setTimeout(function(){
						$("#"+opts.domid).slideUp(opts.anims.speed);
					},stime);
					break;
			};
			setTimeout(function(){
				$("#"+opts.domid).remove();	
			}, opts.anims.speed + stime + 5);
		}
		$.suMessager.closefun = this.closefun;
		//点击关闭函数
		$.suMessager.closefunc = function(obj,type,speed) {
			if(typeof(obj)=="string"){
				var suhide = $(obj);
			}else{
				var suhide = $(obj).parentsUntil('div.suWin_bm').parent('div.suWin_bm');
			}
			if(suhide.length<1){
				return;
			}
			switch (type) {
				case 'slide':
					suhide.slideUp(speed);
					break;
				case 'fade':
					suhide.fadeOut(speed);
					break;
				case 'show':
					suhide.hide(speed);
					break;
				default:
					suhide.slideUp(speed);
					break;
			}
			;
			setTimeout(function(){suhide.remove()}, speed);
		}
		
		//显示弹窗
		this.show = function(opts){
			switch (opts.anims.type) {
				case 'slide':
					$("#"+opts.domid).slideDown(opts.anims.speed);
					break;
				case 'fade':
					$("#"+opts.domid).fadeIn(opts.anims.speed);
					break;
				case 'show':
					$("#"+opts.domid).show(opts.anims.speed);
					break;
				default:
					$("#"+opts.domid).slideDown(opts.anims.speed);
					break;
			}
			var bottomHeight =  "-"+(document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop);
			if($(opts.bindwin).is('div')){
				bottomHeight =  "-"+$("#"+opts.domid).offsetParent().scrollTop();
			}
			$("#"+opts.domid).css("bottom", bottomHeight + "px");
	
			if (opts.time > 0) {
				this.closefun(opts,opts.time);
			}
		}
		
		//初始化
		this.inits(opts);
		this.show(opts);
	};
	return $;
})(jQuery);